﻿using UnityEngine;
using System.Collections;

public class ThrowStone : StateMachineBehaviour
{
    Transform throwStonePoint = null;
    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (throwStonePoint == null)
        {
            throwStonePoint = animator.transform.GetChild(0);
        }     
    }
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetInteger(Constants.Text_AnimState, Constants.AnimState_Kid_Walk);
    }
    public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Vector2 dir = GameManager.Instance.PlayerPlane.transform.position - animator.transform.position;
        Rigidbody2D stone = ObjectPoolManager.Instance.GetStone();
        stone.gameObject.SetActive(true);
        stone.transform.position = throwStonePoint.position;
        dir = GameManager.Instance.PlayerPlane.transform.position - throwStonePoint.position;
        //令扔石头的方向和力度出现一些随机性
        dir += new Vector2(Random.Range(-0.25f, 0.25f), Random.Range(-0.25f, 0.25f));
        stone.AddForce(dir, ForceMode2D.Impulse);
        ObjectPoolManager.Instance.RecircleStone(stone);
    }
}
